package com.yueqian.sort;

import java.util.Arrays;

public class SelectionSort {
    public static void main(String[] args) {
        int[] a = {5, 7, 2, 1, 7, 3, 0, 9};
        selectionSort(a);
        System.out.println(Arrays.toString(a));

    }

    public static void selectionSort(int[] a) {
        selectionSort(a, 0);
    }

    private static void selectionSort(int[] a, int startPosi) {
        //获取起始位置中元素内容
        int minNum = a[startPosi];
        //记录最小元素位置
        int minIndex = startPosi;
        for (int i = startPosi + 1; i < a.length; i++) {
            if (a[i] < minNum) {
                minNum = a[i];
                minIndex = i;
            }
        }
        //交换元素
        int temp = a[startPosi];
        a[startPosi] = a[minIndex];
        a[minIndex] = temp;
        //判断是否已经不需要排序了
        if (startPosi >= a.length - 2) {
            return;
        }
        //递归继续后续元素的排序
        selectionSort(a, ++startPosi);
    }
}
